<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>实现撤销重做 | Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.3.3">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
    

        
    
    
    <link rel="next" href="../../docs/recipes/IsolatingSubapps.html" />
    
    
    <link rel="prev" href="../../docs/recipes/ComputingDerivedData.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="4.7" data-basepath="../.." data-revision="Thu Mar 16 2017 23:41:56 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        自述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="docs/introduction/index.html">
            
                
                    <a href="../../docs/introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="docs/introduction/Motivation.html">
            
                
                    <a href="../../docs/introduction/Motivation.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        动机
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="docs/introduction/CoreConcepts.html">
            
                
                    <a href="../../docs/introduction/CoreConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        核心概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="docs/introduction/ThreePrinciples.html">
            
                
                    <a href="../../docs/introduction/ThreePrinciples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        三大原则
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="docs/introduction/PriorArt.html">
            
                
                    <a href="../../docs/introduction/PriorArt.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        先前技术
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="docs/introduction/Ecosystem.html">
            
                
                    <a href="../../docs/introduction/Ecosystem.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        生态系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="docs/introduction/Examples.html">
            
                
                    <a href="../../docs/introduction/Examples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        示例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="docs/basics/index.html">
            
                
                    <a href="../../docs/basics/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="docs/basics/Actions.html">
            
                
                    <a href="../../docs/basics/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="docs/basics/Reducers.html">
            
                
                    <a href="../../docs/basics/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="docs/basics/Store.html">
            
                
                    <a href="../../docs/basics/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="docs/basics/DataFlow.html">
            
                
                    <a href="../../docs/basics/DataFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="docs/basics/UsageWithReact.html">
            
                
                    <a href="../../docs/basics/UsageWithReact.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        搭配 React
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="docs/basics/ExampleTodoList.html">
            
                
                    <a href="../../docs/basics/ExampleTodoList.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        示例：Todo List
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="docs/advanced/index.html">
            
                
                    <a href="../../docs/advanced/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="docs/advanced/AsyncActions.html">
            
                
                    <a href="../../docs/advanced/AsyncActions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        异步 Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="docs/advanced/AsyncFlow.html">
            
                
                    <a href="../../docs/advanced/AsyncFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        异步数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="docs/advanced/Middleware.html">
            
                
                    <a href="../../docs/advanced/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Middleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="docs/advanced/UsageWithReactRouter.html">
            
                
                    <a href="../../docs/advanced/UsageWithReactRouter.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        搭配 React Router
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="docs/advanced/ExampleRedditAPI.html">
            
                
                    <a href="../../docs/advanced/ExampleRedditAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        示例：Reddit API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="docs/advanced/NextSteps.html">
            
                
                    <a href="../../docs/advanced/NextSteps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        下一步
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="docs/recipes/index.html">
            
                
                    <a href="../../docs/recipes/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        技巧
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="docs/recipes/MigratingToRedux.html">
            
                
                    <a href="../../docs/recipes/MigratingToRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        迁移到 Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="docs/recipes/UsingObjectSpreadOperator.html">
            
                
                    <a href="../../docs/recipes/UsingObjectSpreadOperator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        使用对象展开运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="docs/recipes/ReducingBoilerplate.html">
            
                
                    <a href="../../docs/recipes/ReducingBoilerplate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        减少样板代码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="docs/recipes/ServerRendering.html">
            
                
                    <a href="../../docs/recipes/ServerRendering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        服务端渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="docs/recipes/WritingTests.html">
            
                
                    <a href="../../docs/recipes/WritingTests.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        编写测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="docs/recipes/ComputingDerivedData.html">
            
                
                    <a href="../../docs/recipes/ComputingDerivedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        计算衍生数据
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.7" data-path="docs/recipes/ImplementingUndoHistory.html">
            
                
                    <a href="../../docs/recipes/ImplementingUndoHistory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        实现撤销重做
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="docs/recipes/IsolatingSubapps.html">
            
                
                    <a href="../../docs/recipes/IsolatingSubapps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        子应用隔离
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="docs/recipes/StructuringReducers.html">
            
                
                    <a href="../../docs/recipes/StructuringReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        组织 Reducer
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.9.1" data-path="docs/recipes/reducers/PrerequisiteConcepts.html">
            
                
                    <a href="../../docs/recipes/reducers/PrerequisiteConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.1.</b>
                        
                        Reducer 基础概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.2" data-path="docs/recipes/reducers/BasicReducerStructure.html">
            
                
                    <a href="../../docs/recipes/reducers/BasicReducerStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.2.</b>
                        
                        Reducer 基础结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.3" data-path="docs/recipes/reducers/SplittingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/SplittingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.3.</b>
                        
                        Reducer 逻辑拆分
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.4" data-path="docs/recipes/reducers/RefactoringReducersExample.html">
            
                
                    <a href="../../docs/recipes/reducers/RefactoringReducersExample.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.4.</b>
                        
                        Reducer 重构示例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.5" data-path="docs/recipes/reducers/UsingCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/UsingCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.5.</b>
                        
                        `combineReducers` 用法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.6" data-path="docs/recipes/reducers/BeyondCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/BeyondCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.6.</b>
                        
                        `combineReducers` 进阶
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.7" data-path="docs/recipes/reducers/NormalizingStateShape.html">
            
                
                    <a href="../../docs/recipes/reducers/NormalizingStateShape.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.7.</b>
                        
                        State 范式化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.8" data-path="docs/recipes/reducers/UpdatingNormalizedData.html">
            
                
                    <a href="../../docs/recipes/reducers/UpdatingNormalizedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.8.</b>
                        
                        管理范式化数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.9" data-path="docs/recipes/reducers/ReusingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/ReusingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.9.</b>
                        
                        Reducer 逻辑复用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.10" data-path="docs/recipes/reducers/ImmutableUpdatePatterns.html">
            
                
                    <a href="../../docs/recipes/reducers/ImmutableUpdatePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.10.</b>
                        
                        不可变更新模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.11" data-path="docs/recipes/reducers/InitializingState.html">
            
                
                    <a href="../../docs/recipes/reducers/InitializingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.11.</b>
                        
                        初始化 State
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="docs/FAQ.html">
            
                
                    <a href="../../docs/FAQ.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        常见问题
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="docs/faq/General.html">
            
                
                    <a href="../../docs/faq/General.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        综合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="docs/faq/Reducers.html">
            
                
                    <a href="../../docs/faq/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="docs/faq/OrganizingState.html">
            
                
                    <a href="../../docs/faq/OrganizingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        组织 State
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="docs/faq/StoreSetup.html">
            
                
                    <a href="../../docs/faq/StoreSetup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        创建 Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="docs/faq/Actions.html">
            
                
                    <a href="../../docs/faq/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="docs/faq/CodeStructure.html">
            
                
                    <a href="../../docs/faq/CodeStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        代码结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="docs/faq/Performance.html">
            
                
                    <a href="../../docs/faq/Performance.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        性能
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="docs/faq/ReactRedux.html">
            
                
                    <a href="../../docs/faq/ReactRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        React Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="docs/faq/Miscellaneous.html">
            
                
                    <a href="../../docs/faq/Miscellaneous.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        其它
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="docs/Troubleshooting.html">
            
                
                    <a href="../../docs/Troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        排错
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="docs/Glossary.html">
            
                
                    <a href="../../docs/Glossary.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        词汇表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="docs/api/index.html">
            
                
                    <a href="../../docs/api/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        API 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="docs/api/createStore.html">
            
                
                    <a href="../../docs/api/createStore.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        createStore
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="docs/api/Store.html">
            
                
                    <a href="../../docs/api/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="docs/api/combineReducers.html">
            
                
                    <a href="../../docs/api/combineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        combineReducers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="docs/api/applyMiddleware.html">
            
                
                    <a href="../../docs/api/applyMiddleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        applyMiddleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="docs/api/bindActionCreators.html">
            
                
                    <a href="../../docs/api/bindActionCreators.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        bindActionCreators
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="docs/api/compose.html">
            
                
                    <a href="../../docs/api/compose.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        compose
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="docs/react-redux/index.html">
            
                
                    <a href="../../docs/react-redux/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        react-redux 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="docs/react-redux/api.html">
            
                
                    <a href="../../docs/react-redux/api.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="docs/react-redux/troubleshooting.html">
            
                
                    <a href="../../docs/react-redux/troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        排错
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" >
            
                
                    <a target="_blank" href="https:/github.com/react-guide/redux-tutorial-cn">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        redux-tutorial
                    </a>
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <a id="edit-link" href="https://github.com/camsong/redux-in-chinese/tree/master/docs/recipes/ImplementingUndoHistory.md" class="btn fa fa-edit pull-left">&#xA0;&#xA0;&#x5F00;&#x59CB;&#x7EA0;&#x9519;</a><h1 id="%E5%AE%9E%E7%8E%B0%E6%92%A4%E9%94%80%E5%8E%86%E5%8F%B2">&#x5B9E;&#x73B0;&#x64A4;&#x9500;&#x5386;&#x53F2;</h1>
<p>&#x5728;&#x5E94;&#x7528;&#x4E2D;&#x6784;&#x5EFA;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x529F;&#x80FD;&#x5F80;&#x5F80;&#x9700;&#x8981;&#x5F00;&#x53D1;&#x8005;&#x523B;&#x610F;&#x5730;&#x4ED8;&#x51FA;&#x4E00;&#x4E9B;&#x7CBE;&#x529B;&#x3002;&#x5BF9;&#x4E8E;&#x7ECF;&#x5178;&#x7684; MVC &#x6846;&#x67B6;&#x6765;&#x8BF4;&#xFF0C;&#x8FD9;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x56E0;&#x4E3A;&#x4F60;&#x9700;&#x8981;&#x514B;&#x9686;&#x6240;&#x6709;&#x76F8;&#x5173;&#x7684; model &#x6765;&#x8FFD;&#x8E2A;&#x6BCF;&#x4E00;&#x4E2A;&#x5386;&#x53F2;&#x72B6;&#x6001;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x8003;&#x8651;&#x6574;&#x4E2A;&#x64A4;&#x9500;&#x5806;&#x6808;&#xFF0C;&#x56E0;&#x4E3A;&#x7528;&#x6237;&#x7684;&#x521D;&#x59CB;&#x66F4;&#x6539;&#x4E5F;&#x662F;&#x53EF;&#x64A4;&#x9500;&#x7684;&#x3002;</p>
<p>&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5728; MVC &#x5E94;&#x7528;&#x4E2D;&#x5B9E;&#x73B0;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x529F;&#x80FD;&#x65F6;&#xFF0C;&#x4F60;&#x4E0D;&#x5F97;&#x4E0D;&#x4F7F;&#x7528;&#x4E00;&#x4E9B;&#x7C7B;&#x4F3C;&#x4E8E; <a href="https://en.wikipedia.org/wiki/Command_pattern" target="_blank">Command</a> &#x7684;&#x7279;&#x6B8A;&#x7684;&#x6570;&#x636E;&#x4FEE;&#x6539;&#x6A21;&#x5F0F;&#x6765;&#x91CD;&#x5199;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x7136;&#x800C;&#x4F60;&#x53EF;&#x4EE5;&#x7528; Redux &#x8F7B;&#x800C;&#x6613;&#x4E3E;&#x5730;&#x5B9E;&#x73B0;&#x64A4;&#x9500;&#x5386;&#x53F2;&#xFF0C;&#x56E0;&#x4E3A;&#x4EE5;&#x4E0B;&#x4E09;&#x4E2A;&#x539F;&#x56E0;&#xFF1A;</p>
<ul>
<li>&#x4E0D;&#x5B58;&#x5728;&#x591A;&#x4E2A;&#x6A21;&#x578B;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x5173;&#x5FC3;&#x7684;&#x53EA;&#x662F; state &#x7684;&#x5B50;&#x6811;&#x3002;</li>
<li>state &#x662F;&#x4E0D;&#x53EF;&#x53D8;&#x6570;&#x636E;&#xFF0C;&#x6240;&#x6709;&#x4FEE;&#x6539;&#x88AB;&#x63CF;&#x8FF0;&#x6210;&#x72EC;&#x7ACB;&#x7684; action&#xFF0C;&#x800C;&#x8FD9;&#x4E9B; action &#x4E0E;&#x9884;&#x671F;&#x7684;&#x64A4;&#x9500;&#x5806;&#x6808;&#x6A21;&#x578B;&#x5F88;&#x63A5;&#x8FD1;&#x4E86;&#x3002;</li>
<li>reducer &#x7684;&#x7B7E;&#x540D; <code><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> state</code> &#x53EF;&#x4EE5;&#x81EA;&#x7136;&#x5730;&#x5B9E;&#x73B0; &#x201C;reducer enhancers&#x201D; &#x6216;&#x8005; &#x201C;higher order reducers&#x201D;&#x3002;&#x5B83;&#x4EEC;&#x5728;&#x4F60;&#x4E3A; reducer &#x6DFB;&#x52A0;&#x989D;&#x5916;&#x7684;&#x529F;&#x80FD;&#x65F6;&#x4FDD;&#x6301;&#x7740;&#x8FD9;&#x4E2A;&#x7B7E;&#x540D;&#x3002;&#x64A4;&#x9500;&#x5386;&#x53F2;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5178;&#x578B;&#x7684;&#x5E94;&#x7528;&#x573A;&#x666F;&#x3002;</li>
</ul>
<p>&#x5728;&#x52A8;&#x624B;&#x4E4B;&#x524D;&#xFF0C;&#x786E;&#x8BA4;&#x4F60;&#x5DF2;&#x7ECF;&#x9605;&#x8BFB;&#x8FC7;<a href="../basics/index.html">&#x57FA;&#x7840;&#x6559;&#x7A0B;</a>&#x5E76;&#x4E14;&#x826F;&#x597D;&#x638C;&#x63E1;&#x4E86; <a href="../basics/Reducers.html">reducer &#x5408;&#x6210;</a>&#x3002;&#x672C;&#x6587;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x6784;&#x5EFA;&#x4E8E;<a href="../basics/index.html">&#x57FA;&#x7840;&#x6559;&#x7A0B;</a>&#x7684;&#x793A;&#x4F8B;&#x4E4B;&#x4E0A;&#x3002;</p>
<p>&#x6587;&#x7AE0;&#x7684;&#x7B2C;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x89E3;&#x91CA;&#x5B9E;&#x73B0;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x529F;&#x80FD;&#x6240;&#x7528;&#x5230;&#x7684;&#x57FA;&#x7840;&#x6982;&#x5FF5;&#x3002;</p>
<p>&#x5728;&#x7B2C;&#x4E8C;&#x90E8;&#x5206;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5C55;&#x793A;&#x5982;&#x4F55;&#x4F7F;&#x7528; <a href="https://github.com/omnidan/redux-undo" target="_blank">Redux Undo</a> &#x5E93;&#x6765;&#x65E0;&#x7F1D;&#x5730;&#x5B9E;&#x73B0;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x3002;</p>
<p><a href="https://twitter.com/dan_abramov/status/647038407286390784" target="_blank"><img src="http://i.imgur.com/lvDFHkH.gif" alt="demo of todos-with-undo"></a></p>
<h2 id="%E7%90%86%E8%A7%A3%E6%92%A4%E9%94%80%E5%8E%86%E5%8F%B2">&#x7406;&#x89E3;&#x64A4;&#x9500;&#x5386;&#x53F2;</h2>
<h3 id="%E8%AE%BE%E8%AE%A1%E7%8A%B6%E6%80%81%E7%BB%93%E6%9E%84">&#x8BBE;&#x8BA1;&#x72B6;&#x6001;&#x7ED3;&#x6784;</h3>
<p>&#x64A4;&#x9500;&#x5386;&#x53F2;&#x4E5F;&#x662F;&#x5E94;&#x7528; state &#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x5FC5;&#x8981;&#x4EE5;&#x4E0D;&#x540C;&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x5B83;&#x3002;&#x5F53;&#x4F60;&#x5B9E;&#x73B0;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x65F6;&#xFF0C;&#x65E0;&#x8BBA; state &#x5982;&#x4F55;&#x968F;&#x7740;&#x65F6;&#x95F4;&#x4E0D;&#x65AD;&#x53D8;&#x5316;&#xFF0C;&#x4F60;&#x90FD;&#x9700;&#x8981;&#x8FFD;&#x8E2A; state &#x5728;&#x4E0D;&#x540C;&#x65F6;&#x523B;&#x7684;<strong>&#x5386;&#x53F2;&#x8BB0;&#x5F55;</strong>&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#x5E94;&#x7528;&#x7684; state &#x7ED3;&#x6784;&#x770B;&#x8D77;&#x6765;&#x53EF;&#x80FD;&#x662F;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  counter<span class="token punctuation">:</span> <span class="token number">10</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x5728;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x4E2D;&#x5B9E;&#x73B0;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x7684;&#x8BDD;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x4FDD;&#x5B58;&#x66F4;&#x591A;&#x7684; state &#x4EE5;&#x89E3;&#x51B3;&#x4E0B;&#x9762;&#x51E0;&#x4E2A;&#x95EE;&#x9898;&#xFF1A;</p>
<ul>
<li>&#x64A4;&#x9500;&#x6216;&#x91CD;&#x505A;&#x7559;&#x4E0B;&#x4E86;&#x54EA;&#x4E9B;&#x4FE1;&#x606F;&#xFF1F;</li>
<li>&#x5F53;&#x524D;&#x7684;&#x72B6;&#x6001;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</li>
<li>&#x64A4;&#x9500;&#x5806;&#x6808;&#x4E2D;&#x8FC7;&#x53BB;&#xFF08;&#x548C;&#x672A;&#x6765;&#xFF09;&#x7684;&#x72B6;&#x6001;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</li>
</ul>
<p>&#x4E3A;&#x6B64;&#x6211;&#x4EEC;&#x5BF9; state &#x7ED3;&#x6784;&#x505A;&#x4E86;&#x4EE5;&#x4E0B;&#x4FEE;&#x6539;&#x4EE5;&#x4FBF;&#x89E3;&#x51B3;&#x4E0A;&#x8FF0;&#x95EE;&#x9898;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  counter<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;&#x5982;&#x679C;&#x6309;&#x4E0B;&#x201C;&#x64A4;&#x9500;&#x201D;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x6062;&#x590D;&#x5230;&#x8FC7;&#x53BB;&#x7684;&#x72B6;&#x6001;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  counter<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token number">9</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">10</span> <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x518D;&#x6309;&#x4E00;&#x6B21;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  counter<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token number">8</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span> <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5F53;&#x6211;&#x4EEC;&#x6309;&#x4E0B;&#x201C;&#x91CD;&#x505A;&#x201D;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x5F80;&#x672A;&#x6765;&#x7684;&#x72B6;&#x6001;&#x79FB;&#x52A8;&#x4E00;&#x6B65;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  counter<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token number">9</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">10</span> <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6700;&#x7EC8;&#xFF0C;&#x5F53;&#x5904;&#x4E8E;&#x64A4;&#x9500;&#x5806;&#x6808;&#x4E2D;&#x65F6;&#xFF0C;&#x7528;&#x6237;&#x53D1;&#x8D77;&#x4E86;&#x4E00;&#x4E2A;&#x64CD;&#x4F5C;&#xFF08;&#x4F8B;&#x5982;&#xFF0C;&#x51CF;&#x5C11;&#x8BA1;&#x6570;&#xFF09;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x4E22;&#x5F03;&#x6240;&#x6709;&#x672A;&#x6765;&#x7684;&#x4FE1;&#x606F;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  counter<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token number">8</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6709;&#x8DA3;&#x7684;&#x4E00;&#x70B9;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x64A4;&#x9500;&#x5806;&#x6808;&#x4E2D;&#x4FDD;&#x5B58;&#x7684;&#x662F;&#x6570;&#x5B57;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x6570;&#x7EC4;&#x6216;&#x662F;&#x5BF9;&#x8C61;&#x90FD;&#x4E0D;&#x91CD;&#x8981;&#xFF0C;&#x56E0;&#x4E3A;&#x6574;&#x4E2A;&#x7ED3;&#x6784;&#x59CB;&#x7EC8;&#x4FDD;&#x6301;&#x4E00;&#x81F4;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  counter<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">4</span> <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  todos<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Use Redux&apos;</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">,</span> complete<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">,</span> complete<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Implement Undo&apos;</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">[</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">,</span> complete<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Implement Undo&apos;</span><span class="token punctuation">,</span> complete<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5B83;&#x770B;&#x8D77;&#x6765;&#x901A;&#x5E38;&#x90FD;&#x662F;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  past<span class="token punctuation">:</span> Array<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token punctuation">,</span>
  present<span class="token punctuation">:</span> T<span class="token punctuation">,</span>
  future<span class="token punctuation">:</span> Array<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;&#x9876;&#x5C42;&#x4FDD;&#x5B58;&#x5355;&#x4E00;&#x7684;&#x5386;&#x53F2;&#x8BB0;&#x5F55;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  past<span class="token punctuation">:</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span> counterA<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> counterB<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> counterA<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> counterB<span class="token punctuation">:</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> counterA<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> counterB<span class="token punctuation">:</span> <span class="token number">0</span> <span class="token punctuation">}</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
  present<span class="token punctuation">:</span> <span class="token punctuation">{</span> counterA<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> counterB<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  future<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x5206;&#x79BB;&#x5386;&#x53F2;&#x8BB0;&#x5F55;&#xFF0C;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x72EC;&#x7ACB;&#x5730;&#x6267;&#x884C;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x64CD;&#x4F5C;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  counterA<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  counterB<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x770B;&#x5230;&#x5982;&#x4F55;&#x5408;&#x9002;&#x5730;&#x5206;&#x79BB;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x3002;</p>
<h3 id="%E8%AE%BE%E8%AE%A1%E7%AE%97%E6%B3%95">&#x8BBE;&#x8BA1;&#x7B97;&#x6CD5;</h3>
<p>&#x65E0;&#x8BBA;&#x4F55;&#x79CD;&#x7279;&#x5B9A;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x91CD;&#x505A;&#x5386;&#x53F2;&#x8BB0;&#x5F55;&#x7684; state &#x7ED3;&#x6784;&#x59CB;&#x7EC8;&#x4E00;&#x81F4;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  past<span class="token punctuation">:</span> Array<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token punctuation">,</span>
  present<span class="token punctuation">:</span> T<span class="token punctuation">,</span>
  future<span class="token punctuation">:</span> Array<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x8BA8;&#x8BBA;&#x4E00;&#x4E0B;&#x5982;&#x4F55;&#x901A;&#x8FC7;&#x7B97;&#x6CD5;&#x6765;&#x64CD;&#x4F5C;&#x4E0A;&#x6587;&#x6240;&#x8FF0;&#x7684; state &#x7ED3;&#x6784;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x4E24;&#x4E2A; action &#x6765;&#x64CD;&#x4F5C;&#x8BE5; state&#xFF1A;<code>UNDO</code> &#x548C; <code>REDO</code>&#x3002;&#x5728; reducer &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x4EE5;&#x5982;&#x4E0B;&#x6B65;&#x9AA4;&#x5904;&#x7406;&#x8FD9;&#x4E24;&#x4E2A; action&#xFF1A;</p>
<h4 id="%E5%A4%84%E7%90%86-undo">&#x5904;&#x7406; Undo</h4>
<ul>
<li>&#x79FB;&#x9664; <code>past</code> &#x4E2D;&#x7684;<strong>&#x6700;&#x540E;&#x4E00;&#x4E2A;</strong>&#x5143;&#x7D20;&#x3002;</li>
<li>&#x5C06;&#x4E0A;&#x4E00;&#x6B65;&#x79FB;&#x9664;&#x7684;&#x5143;&#x7D20;&#x8D4B;&#x4E88; <code>present</code>&#x3002;</li>
<li>&#x5C06;&#x539F;&#x6765;&#x7684; <code>present</code> &#x63D2;&#x5165;&#x5230; <code>future</code> &#x7684;<strong>&#x6700;&#x524D;&#x9762;</strong>&#x3002;</li>
</ul>
<h4 id="%E5%A4%84%E7%90%86-redo">&#x5904;&#x7406; Redo</h4>
<ul>
<li>&#x79FB;&#x9664; <code>future</code> &#x4E2D;&#x7684;<strong>&#x7B2C;&#x4E00;&#x4E2A;</strong>&#x5143;&#x7D20;&#x3002;</li>
<li>&#x5C06;&#x4E0A;&#x4E00;&#x6B65;&#x79FB;&#x9664;&#x7684;&#x5143;&#x7D20;&#x8D4B;&#x4E88; <code>present</code>&#x3002;</li>
<li>&#x5C06;&#x539F;&#x6765;&#x7684; <code>present</code> &#x8FFD;&#x52A0;&#x5230; <code>past</code> &#x7684;<strong>&#x6700;&#x540E;&#x9762;</strong>&#x3002;</li>
</ul>
<h4 id="%E5%A4%84%E7%90%86%E5%85%B6%E4%BB%96-action">&#x5904;&#x7406;&#x5176;&#x4ED6; Action</h4>
<ul>
<li>&#x5C06;&#x5F53;&#x524D;&#x7684; <code>present</code> &#x8FFD;&#x52A0;&#x5230; <code>past</code> &#x7684;<strong>&#x6700;&#x540E;&#x9762;</strong>&#x3002;</li>
<li>&#x5C06;&#x5904;&#x7406;&#x5B8C; action &#x6240;&#x4EA7;&#x751F;&#x7684;&#x65B0;&#x7684; state &#x8D4B;&#x4E88; <code>present</code>&#x3002;</li>
<li>&#x6E05;&#x7A7A; <code>future</code>&#x3002;</li>
</ul>
<h3 id="%E7%AC%AC%E4%B8%80%E6%AC%A1%E5%B0%9D%E8%AF%95-%E7%BC%96%E5%86%99-reducer">&#x7B2C;&#x4E00;&#x6B21;&#x5C1D;&#x8BD5;: &#x7F16;&#x5199; Reducer</h3>
<pre><code class="lang-js"><span class="token keyword">const</span> initialState <span class="token operator">=</span> <span class="token punctuation">{</span>
  past<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  present<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">// (?) &#x6211;&#x4EEC;&#x5982;&#x4F55;&#x521D;&#x59CB;&#x5316;&#x5F53;&#x524D;&#x72B6;&#x6001;?</span>
  future<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">undoable</span><span class="token punctuation">(</span>state <span class="token operator">=</span> initialState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token punctuation">{</span> past<span class="token punctuation">,</span> present<span class="token punctuation">,</span> future <span class="token punctuation">}</span> <span class="token operator">=</span> state<span class="token punctuation">;</span>

  <span class="token keyword">switch</span> <span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token string">&apos;UNDO&apos;</span><span class="token punctuation">:</span>
      <span class="token keyword">const</span> previous <span class="token operator">=</span> past<span class="token punctuation">[</span>past<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span>
      <span class="token keyword">const</span> newPast <span class="token operator">=</span> past<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> past<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>
      <span class="token keyword">return</span> <span class="token punctuation">{</span>
        past<span class="token punctuation">:</span> newPast<span class="token punctuation">,</span>
        present<span class="token punctuation">:</span> previous<span class="token punctuation">,</span>
        future<span class="token punctuation">:</span> <span class="token punctuation">[</span> present<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>future <span class="token punctuation">]</span>
      <span class="token punctuation">}</span>
    <span class="token keyword">case</span> <span class="token string">&apos;REDO&apos;</span><span class="token punctuation">:</span>
      <span class="token keyword">const</span> next <span class="token operator">=</span> future<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
      <span class="token keyword">const</span> newFuture <span class="token operator">=</span> future<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
      <span class="token keyword">return</span> <span class="token punctuation">{</span>
        past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>past<span class="token punctuation">,</span> present <span class="token punctuation">]</span><span class="token punctuation">,</span>
        present<span class="token punctuation">:</span> next<span class="token punctuation">,</span>
        future<span class="token punctuation">:</span> newFuture
      <span class="token punctuation">}</span>
    <span class="token keyword">default</span><span class="token punctuation">:</span>
      <span class="token comment" spellcheck="true">// (?) &#x6211;&#x4EEC;&#x5982;&#x4F55;&#x5904;&#x7406;&#x5176;&#x4ED6; action&#xFF1F;</span>
      <span class="token keyword">return</span> state
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x5B9E;&#x73B0;&#x662F;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5FFD;&#x7565;&#x4E86;&#x4E0B;&#x9762;&#x4E09;&#x4E2A;&#x91CD;&#x8981;&#x7684;&#x95EE;&#x9898;&#xFF1A;</p>
<ul>
<li>&#x6211;&#x4EEC;&#x4ECE;&#x4F55;&#x5904;&#x83B7;&#x53D6;&#x521D;&#x59CB;&#x7684; <code>present</code> &#x72B6;&#x6001;&#xFF1F;&#x6211;&#x4EEC;&#x65E0;&#x6CD5;&#x9884;&#x5148;&#x77E5;&#x9053;&#x5B83;&#x3002;</li>
<li>&#x5F53;&#x5904;&#x7406;&#x5B8C;&#x5916;&#x90E8;&#x7684; action &#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x54EA;&#x91CC;&#x5B8C;&#x6210;&#x5C06; <code>present</code> &#x4FDD;&#x5B58;&#x5230; <code>past</code> &#x7684;&#x5DE5;&#x4F5C;&#xFF1F;</li>
<li>&#x6211;&#x4EEC;&#x5982;&#x4F55;&#x5C06; <code>present</code> &#x72B6;&#x6001;&#x7684;&#x63A7;&#x5236;&#x59D4;&#x6258;&#x7ED9;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x7684; reducer&#xFF1F;</li>
</ul>
<p>&#x770B;&#x8D77;&#x6765; reducer &#x5E76;&#x4E0D;&#x662F;&#x6B63;&#x786E;&#x7684;&#x62BD;&#x8C61;&#x65B9;&#x5F0F;&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x975E;&#x5E38;&#x63A5;&#x8FD1;&#x4E86;&#x3002;</p>
<h3 id="%E5%88%9D%E8%AF%86-reducer-enhancers">&#x521D;&#x8BC6; Reducer Enhancers</h3>
<p>&#x4F60;&#x53EF;&#x80FD;&#x5DF2;&#x7ECF;&#x719F;&#x6089; <a href="https://en.wikipedia.org/wiki/Higher-order_function" target="_blank">higher order function</a> &#x4E86;&#x3002;&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x8FC7; React&#xFF0C;&#x4E5F;&#x5E94;&#x8BE5;&#x719F;&#x6089; <a href="https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750" target="_blank">higher order component</a>&#x3002;&#x6211;&#x4EEC;&#x628A;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x52A0;&#x5DE5;&#x4E00;&#x4E0B;&#xFF0C;&#x5C06;&#x5176;&#x8FD0;&#x7528;&#x5230; reducers&#x3002;</p>
<p><strong>reducer enhancer</strong>&#xFF08;&#x6216;&#x8005; <strong>higher order reducer</strong>&#xFF09;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x63A5;&#x6536; reducer &#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684; reducer&#xFF0C;&#x8FD9;&#x4E2A;&#x65B0;&#x7684; reducer &#x53EF;&#x4EE5;&#x5904;&#x7406;&#x65B0;&#x7684; action&#xFF0C;&#x6216;&#x8005;&#x7EF4;&#x62A4;&#x66F4;&#x591A;&#x7684; state&#xFF0C;&#x4EA6;&#x6216;&#x8005;&#x5C06;&#x5B83;&#x65E0;&#x6CD5;&#x5904;&#x7406;&#x7684; action &#x59D4;&#x6258;&#x7ED9;&#x539F;&#x59CB;&#x7684; reducer &#x5904;&#x7406;&#x3002;&#x8FD9;&#x4E0D;&#x662F;&#x4EC0;&#x4E48;&#x65B0;&#x6A21;&#x5F0F;&#xFF0C;<a href="../api/combineReducers.html"><code><span class="token function">combineReducers</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a>&#x4E5F;&#x662F; reducer enhancer&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x540C;&#x6837;&#x63A5;&#x6536;&#x591A;&#x4E2A; reducer &#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684; reducer&#x3002;</p>
<p>&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x529F;&#x80FD;&#x7684; reducer enhancer &#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">doNothingWith</span><span class="token punctuation">(</span>reducer<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// &#x4EC5;&#x4EC5;&#x8C03;&#x7528;&#x4F20;&#x5165;&#x7684; reducer</span>
    <span class="token keyword">return</span> <span class="token function">reducer</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E00;&#x4E2A;&#x7EC4;&#x5408;&#x5176;&#x4ED6; reducer &#x7684; reducer enhancer &#x770B;&#x8D77;&#x6765;&#x7C7B;&#x4F3C;&#x4E8E;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">combineReducers</span><span class="token punctuation">(</span>reducers<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>state <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>reducers<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span>nextState<span class="token punctuation">,</span> key<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
      <span class="token comment" spellcheck="true">// &#x8C03;&#x7528;&#x6BCF;&#x4E00;&#x4E2A; reducer &#x5E76;&#x5C06;&#x5176;&#x7BA1;&#x7406;&#x7684;&#x90E8;&#x5206; state &#x4F20;&#x7ED9;&#x5B83;</span>
      nextState<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">=</span> reducers<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">(</span>state<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
      <span class="token keyword">return</span> nextState
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="%E7%AC%AC%E4%BA%8C%E6%AC%A1%E5%B0%9D%E8%AF%95-%E7%BC%96%E5%86%99-reducer-enhancer">&#x7B2C;&#x4E8C;&#x6B21;&#x5C1D;&#x8BD5;: &#x7F16;&#x5199; Reducer Enhancer</h3>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5BF9; reducer enhancer &#x6709;&#x4E86;&#x66F4;&#x6DF1;&#x7684;&#x4E86;&#x89E3;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x660E;&#x786E;&#x6240;&#x8C13;&#x7684;<code>&#x53EF;&#x64A4;&#x9500;</code>&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">undoable</span><span class="token punctuation">(</span>reducer<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// &#x4EE5;&#x4E00;&#x4E2A;&#x7A7A;&#x7684; action &#x8C03;&#x7528; reducer &#x6765;&#x4EA7;&#x751F;&#x521D;&#x59CB;&#x7684; state</span>
  <span class="token keyword">const</span> initialState <span class="token operator">=</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token function">reducer</span><span class="token punctuation">(</span>undefined<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span>

  <span class="token comment" spellcheck="true">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x7684;&#x65B0;&#x7684;reducer</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>state <span class="token operator">=</span> initialState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> past<span class="token punctuation">,</span> present<span class="token punctuation">,</span> future <span class="token punctuation">}</span> <span class="token operator">=</span> state<span class="token punctuation">;</span>

    <span class="token keyword">switch</span> <span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">case</span> <span class="token string">&apos;UNDO&apos;</span><span class="token punctuation">:</span>
        <span class="token keyword">const</span> previous <span class="token operator">=</span> past<span class="token punctuation">[</span>past<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span>
        <span class="token keyword">const</span> newPast <span class="token operator">=</span> past<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> past<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token punctuation">{</span>
          past<span class="token punctuation">:</span> newPast<span class="token punctuation">,</span>
          present<span class="token punctuation">:</span> previous<span class="token punctuation">,</span>
          future<span class="token punctuation">:</span> <span class="token punctuation">[</span> present<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>future <span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
      <span class="token keyword">case</span> <span class="token string">&apos;REDO&apos;</span><span class="token punctuation">:</span>
        <span class="token keyword">const</span> next <span class="token operator">=</span> future<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
        <span class="token keyword">const</span> newFuture <span class="token operator">=</span> future<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token punctuation">{</span>
          past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>past<span class="token punctuation">,</span> present <span class="token punctuation">]</span><span class="token punctuation">,</span>
          present<span class="token punctuation">:</span> next<span class="token punctuation">,</span>
          future<span class="token punctuation">:</span> newFuture
        <span class="token punctuation">}</span>
      <span class="token keyword">default</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true">// &#x5C06;&#x5176;&#x4ED6; action &#x59D4;&#x6258;&#x7ED9;&#x539F;&#x59CB;&#x7684; reducer &#x5904;&#x7406;</span>
        <span class="token keyword">const</span> newPresent <span class="token operator">=</span> <span class="token function">reducer</span><span class="token punctuation">(</span>present<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>present <span class="token operator">===</span> newPresent<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token keyword">return</span> state
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token punctuation">{</span>
          past<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>past<span class="token punctuation">,</span> present <span class="token punctuation">]</span><span class="token punctuation">,</span>
          present<span class="token punctuation">:</span> newPresent<span class="token punctuation">,</span>
          future<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x5C06;&#x4EFB;&#x610F;&#x7684; reducer &#x5C01;&#x88C5;&#x5230;<code>&#x53EF;&#x64A4;&#x9500;</code>&#x7684; reducer enhancer&#xFF0C;&#x4ECE;&#x800C;&#x5904;&#x7406; <code>UNDO</code> &#x548C; <code>REDO</code> &#x8FD9;&#x4E24;&#x4E2A; action&#x3002;</p>
<pre><code class="lang-js"><span class="token comment" spellcheck="true">// &#x8FD9;&#x662F;&#x4E00;&#x4E2A; reducer&#x3002;</span>
<span class="token keyword">function</span> <span class="token function">todos</span><span class="token punctuation">(</span>state <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">/* ... */</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// &#x5904;&#x7406;&#x5B8C;&#x6210;&#x4E4B;&#x540E;&#x4ECD;&#x7136;&#x662F;&#x4E00;&#x4E2A; reducer&#xFF01;</span>
<span class="token keyword">const</span> undoableTodos <span class="token operator">=</span> <span class="token function">undoable</span><span class="token punctuation">(</span>todos<span class="token punctuation">)</span>

<span class="token keyword">import</span> <span class="token punctuation">{</span> createStore <span class="token punctuation">}</span> from <span class="token string">&apos;redux&apos;</span>
<span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>undoableTodos<span class="token punctuation">)</span>

store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  type<span class="token punctuation">:</span> <span class="token string">&apos;ADD_TODO&apos;</span><span class="token punctuation">,</span>
  text<span class="token punctuation">:</span> <span class="token string">&apos;Use Redux&apos;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  type<span class="token punctuation">:</span> <span class="token string">&apos;ADD_TODO&apos;</span><span class="token punctuation">,</span>
  text<span class="token punctuation">:</span> <span class="token string">&apos;Implement Undo&apos;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  type<span class="token punctuation">:</span> <span class="token string">&apos;UNDO&apos;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<p>&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x91CD;&#x8981;&#x6CE8;&#x610F;&#x70B9;&#xFF1A;&#x4F60;&#x9700;&#x8981;&#x8BB0;&#x4F4F;&#x5F53;&#x4F60;&#x6062;&#x590D;&#x4E00;&#x4E2A; state &#x65F6;&#xFF0C;&#x5FC5;&#x987B;&#x628A; <code><span class="token punctuation">.</span>present</code> &#x8FFD;&#x52A0;&#x5230;&#x5F53;&#x524D;&#x7684; state &#x4E0A;&#x3002;&#x4F60;&#x4E5F;&#x4E0D;&#x80FD;&#x5FD8;&#x4E86;&#x901A;&#x8FC7;&#x68C0;&#x67E5; <code><span class="token punctuation">.</span>past<span class="token punctuation">.</span>length</code> &#x548C; <code><span class="token punctuation">.</span>future<span class="token punctuation">.</span>length</code> &#x786E;&#x5B9A;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x6309;&#x94AE;&#x662F;&#x5426;&#x53EF;&#x7528;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x80FD;&#x542C;&#x8BF4;&#x8FC7; Redux &#x53D7; <a href="https://github.com/evancz/elm-architecture-tutorial/" target="_blank">Elm &#x67B6;&#x6784;</a> &#x5F71;&#x54CD;&#x9887;&#x6DF1;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x5FC5;&#x60CA;&#x8BB6;&#x4E8E;&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x4E0E; <a href="http://package.elm-lang.org/packages/TheSeamau5/elm-undo-redo/2.0.0" target="_blank">elm-undo-redo package</a> &#x5982;&#x6B64;&#x76F8;&#x4F3C;&#x3002;</p>
<h2 id="%E4%BD%BF%E7%94%A8-redux-undo">&#x4F7F;&#x7528; Redux Undo</h2>
<p>&#x4EE5;&#x4E0A;&#x8FD9;&#x4E9B;&#x4FE1;&#x606F;&#x90FD;&#x975E;&#x5E38;&#x6709;&#x7528;&#xFF0C;&#x4F46;&#x662F;&#x6709;&#x6CA1;&#x6709;&#x4E00;&#x4E2A;&#x5E93;&#x80FD;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;<code>&#x53EF;&#x64A4;&#x9500;</code>&#x529F;&#x80FD;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x7531;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x7F16;&#x5199;&#x5462;&#xFF1F;&#x5F53;&#x7136;&#x6709;&#xFF01;&#x6765;&#x770B;&#x770B; <a href="https://github.com/omnidan/redux-undo" target="_blank">Redux Undo</a>&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x4E3A;&#x4F60;&#x7684; Redux &#x72B6;&#x6001;&#x6811;&#x4E2D;&#x7684;&#x4EFB;&#x4F55;&#x90E8;&#x5206;&#x63D0;&#x4F9B;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x529F;&#x80FD;&#x3002;</p>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x90E8;&#x5206;&#x4E2D;&#xFF0C;&#x4F60;&#x4F1A;&#x5B66;&#x5230;&#x5982;&#x4F55;&#x8BA9; <a href="../basics/ExampleTodoList.html">&#x793A;&#x4F8B;&#xFF1A;Todo List</a> &#x62E5;&#x6709;&#x53EF;&#x64A4;&#x9500;&#x7684;&#x529F;&#x80FD;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5728; <a href="https://github.com/reactjs/redux/tree/master/examples/todos-with-undo" target="_blank"><code>todos<span class="token operator">-</span><span class="token keyword">with</span><span class="token operator">-</span>undo</code></a>&#x627E;&#x5230;&#x5B8C;&#x6574;&#x7684;&#x6E90;&#x7801;&#x3002;</p>
<h3 id="%E5%AE%89%E8%A3%85">&#x5B89;&#x88C5;</h3>
<p>&#x9996;&#x5148;&#xFF0C;&#x4F60;&#x5FC5;&#x987B;&#x5148;&#x6267;&#x884C;</p>
<pre><code>npm install <span class="token operator">--</span>save redux<span class="token operator">-</span>undo
</code></pre><p>&#x8FD9;&#x4E00;&#x6B65;&#x4F1A;&#x5B89;&#x88C5;&#x4E00;&#x4E2A;&#x63D0;&#x4F9B;<code>&#x53EF;&#x64A4;&#x9500;</code>&#x529F;&#x80FD;&#x7684; reducer enhancer &#x7684;&#x5E93;&#x3002;</p>
<h3 id="%E5%B0%81%E8%A3%85-reducer">&#x5C01;&#x88C5; Reducer</h3>
<p>&#x4F60;&#x9700;&#x8981;&#x901A;&#x8FC7; <code>undoable</code> &#x51FD;&#x6570;&#x5F3A;&#x5316;&#x4F60;&#x7684; reducer&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5982;&#x679C;&#x4E4B;&#x524D;&#x5BFC;&#x51FA;&#x7684;&#x662F; todos reducer&#xFF0C;&#x90A3;&#x4E48;&#x73B0;&#x5728;&#x4F60;&#x9700;&#x8981;&#x628A;&#x8FD9;&#x4E2A; reducer &#x4F20;&#x7ED9; <code><span class="token function">undoable</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x7136;&#x540E;&#x628A;&#x8BA1;&#x7B97;&#x7ED3;&#x679C;&#x5BFC;&#x51FA;&#xFF1A;</p>
<h4 id="-reducers-todos-js"><code>reducers<span class="token operator">/</span>todos<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> undoable<span class="token punctuation">,</span> <span class="token punctuation">{</span> distinctState <span class="token punctuation">}</span> from <span class="token string">&apos;redux-undo&apos;</span>

<span class="token comment" spellcheck="true">/* ... */</span>

<span class="token keyword">const</span> todos <span class="token operator">=</span> <span class="token punctuation">(</span>state <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">/* ... */</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> undoableTodos <span class="token operator">=</span> <span class="token function">undoable</span><span class="token punctuation">(</span>todos<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  filter<span class="token punctuation">:</span> <span class="token function">distinctState</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> undoableTodos
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684; <code><span class="token function">distinctState</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x8FC7;&#x6EE4;&#x5668;&#x4F1A;&#x5FFD;&#x7565;&#x90A3;&#x4E9B;&#x6CA1;&#x6709;&#x5F15;&#x8D77; state &#x53D8;&#x5316;&#x7684; actions&#xFF0C;&#x53EF;&#x64A4;&#x9500;&#x7684; reducer &#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<a href="https://github.com/omnidan/redux-undo#configuration" target="_blank">&#x5176;&#x4ED6;&#x9009;&#x62E9;</a>&#x8FDB;&#x884C;&#x914D;&#x7F6E;&#xFF0C;&#x4F8B;&#x5982;&#x4E3A;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x7684; action &#x8BBE;&#x7F6E; action type&#x3002;</p>
<p>&#x503C;&#x5F97;&#x6CE8;&#x610F;&#x7684;&#x662F;&#x867D;&#x7136;&#x8FD9;&#x4E0E;&#x8C03;&#x7528; <code><span class="token function">combineReducers</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x7684;&#x7ED3;&#x679C;&#x522B;&#x65E0;&#x4E8C;&#x81F4;&#xFF0C;&#x4F46;&#x662F;&#x73B0;&#x5728;&#x7684; <code>todos</code> reducer &#x53EF;&#x4EE5;&#x4F20;&#x9012;&#x7ED9; Redux Undo &#x589E;&#x5F3A;&#x7684; reducer&#x3002;</p>
<h4 id="-reducers-index-js"><code>reducers<span class="token operator">/</span>index<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> combineReducers <span class="token punctuation">}</span> from <span class="token string">&apos;redux&apos;</span>
<span class="token keyword">import</span> todos from <span class="token string">&apos;./todos&apos;</span>
<span class="token keyword">import</span> visibilityFilter from <span class="token string">&apos;./visibilityFilter&apos;</span>

<span class="token keyword">const</span> todoApp <span class="token operator">=</span> <span class="token function">combineReducers</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  todos<span class="token punctuation">,</span>
  visibilityFilter
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> todoApp
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728; reducer &#x5408;&#x5E76;&#x5C42;&#x6B21;&#x4E2D;&#x7684;&#x4EFB;&#x4F55;&#x5C42;&#x7EA7;&#x5BF9;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A; reducer &#x6267;&#x884C; <code>undoable</code>&#x3002;&#x6211;&#x4EEC;&#x53EA;&#x5BF9; <code>todos</code> reducer &#x8FDB;&#x884C;&#x5C01;&#x88C5;&#x800C;&#x4E0D;&#x662F;&#x6574;&#x4E2A;&#x9876;&#x5C42;&#x7684; reducer&#xFF0C;&#x8FD9;&#x6837; <code>visibilityFilter</code> &#x5F15;&#x8D77;&#x7684;&#x53D8;&#x5316;&#x624D;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x64A4;&#x9500;&#x5386;&#x53F2;&#x3002;</p>
<h3 id="%E6%9B%B4%E6%96%B0-selectors">&#x66F4;&#x65B0; Selectors</h3>
<p>&#x73B0;&#x5728; <code>todos</code> &#x76F8;&#x5173;&#x7684; state &#x770B;&#x8D77;&#x6765;&#x5E94;&#x8BE5;&#x50CF;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  visibilityFilter<span class="token punctuation">:</span> <span class="token string">&apos;SHOW_ALL&apos;</span><span class="token punctuation">,</span>
  todos<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    past<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Use Redux&apos;</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">,</span> complete<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    present<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">,</span> complete<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Implement Undo&apos;</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    future<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">[</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">,</span> complete<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> text<span class="token punctuation">:</span> <span class="token string">&apos;Implement Undo&apos;</span><span class="token punctuation">,</span> complete<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x610F;&#x5473;&#x7740;&#x4F60;&#x5FC5;&#x987B;&#x901A;&#x8FC7; <code>state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span>present</code> &#x8BBF;&#x95EE; state &#x800C;&#x4E0D;&#x662F;&#x539F;&#x6765;&#x7684; <code>state<span class="token punctuation">.</span>todos</code>&#xFF1A;</p>
<h4 id="-containers-visibletodolist-js"><code>containers<span class="token operator">/</span>VisibleTodoList<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">const</span> mapStateToProps <span class="token operator">=</span> <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    todos<span class="token punctuation">:</span> <span class="token function">getVisibleTodos</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span>present<span class="token punctuation">,</span> state<span class="token punctuation">.</span>visibilityFilter<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="%E6%B7%BB%E5%8A%A0%E6%8C%89%E9%92%AE">&#x6DFB;&#x52A0;&#x6309;&#x94AE;</h3>
<p>&#x73B0;&#x5728;&#x53EA;&#x5269;&#x4E0B;&#x7ED9;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x7684; action &#x6DFB;&#x52A0;&#x6309;&#x94AE;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x4E3A;&#x8FD9;&#x4E9B;&#x6309;&#x94AE;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; <code>UndoRedo</code> &#x7684;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x3002;&#x7531;&#x4E8E;&#x5C55;&#x793A;&#x90E8;&#x5206;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x518D;&#x9700;&#x8981;&#x628A;&#x5B83;&#x4EEC;&#x5206;&#x79BB;&#x5230;&#x5355;&#x72EC;&#x7684;&#x6587;&#x4EF6;&#x53BB;&#xFF1A;</p>
<h4 id="-containers-undoredo-js"><code>containers<span class="token operator">/</span>UndoRedo<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React from <span class="token string">&apos;react&apos;</span>

<span class="token comment" spellcheck="true">/* ... */</span>

<span class="token keyword">let</span> UndoRedo <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> canUndo<span class="token punctuation">,</span> canRedo<span class="token punctuation">,</span> onUndo<span class="token punctuation">,</span> onRedo <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>
  <span class="token operator">&lt;</span>p<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>button onClick<span class="token operator">=</span><span class="token punctuation">{</span>onUndo<span class="token punctuation">}</span> disabled<span class="token operator">=</span><span class="token punctuation">{</span><span class="token operator">!</span>canUndo<span class="token punctuation">}</span><span class="token operator">&gt;</span>
      Undo
    <span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>button onClick<span class="token operator">=</span><span class="token punctuation">{</span>onRedo<span class="token punctuation">}</span> disabled<span class="token operator">=</span><span class="token punctuation">{</span><span class="token operator">!</span>canRedo<span class="token punctuation">}</span><span class="token operator">&gt;</span>
      Redo
    <span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>
<span class="token punctuation">)</span>
</code></pre>
<p>&#x4F60;&#x9700;&#x8981;&#x4F7F;&#x7528; <a href="https://github.com/reactjs/react-redux" target="_blank">React Redux</a> &#x7684; connect &#x51FD;&#x6570;&#x751F;&#x6210;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#xFF0C;&#x7136;&#x540E;&#x68C0;&#x67E5; <code>state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span>past<span class="token punctuation">.</span>length</code> &#x548C; <code>state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span>future<span class="token punctuation">.</span>length</code> &#x6765;&#x5224;&#x65AD;&#x662F;&#x5426;&#x542F;&#x7528;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x6309;&#x94AE;&#x3002;&#x4F60;&#x4E0D;&#x518D;&#x9700;&#x8981;&#x7ED9;&#x64A4;&#x9500;&#x548C;&#x91CD;&#x505A;&#x7F16;&#x5199; action creators &#x4E86;&#xFF0C;&#x56E0;&#x4E3A; Redux Undo &#x5DF2;&#x7ECF;&#x63D0;&#x4F9B;&#x4E86;&#x8FD9;&#x4E9B; action creators&#xFF1A;</p>
<h4 id="-containers-undoredo-js"><code>containers<span class="token operator">/</span>UndoRedo<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token comment" spellcheck="true">/* ... */</span>

<span class="token keyword">import</span> <span class="token punctuation">{</span> ActionCreators as UndoActionCreators <span class="token punctuation">}</span> from <span class="token string">&apos;redux-undo&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> connect <span class="token punctuation">}</span> from <span class="token string">&apos;react-redux&apos;</span>

<span class="token comment" spellcheck="true">/* ... */</span>

<span class="token keyword">const</span> mapStateToProps <span class="token operator">=</span> <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    canUndo<span class="token punctuation">:</span> state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span>past<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">,</span>
    canRedo<span class="token punctuation">:</span> state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span>future<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> mapDispatchToProps <span class="token operator">=</span> <span class="token punctuation">(</span>dispatch<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    onUndo<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">dispatch</span><span class="token punctuation">(</span>UndoActionCreators<span class="token punctuation">.</span><span class="token function">undo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    onRedo<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">dispatch</span><span class="token punctuation">(</span>UndoActionCreators<span class="token punctuation">.</span><span class="token function">redo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

UndoRedo <span class="token operator">=</span> <span class="token function">connect</span><span class="token punctuation">(</span>
  mapStateToProps<span class="token punctuation">,</span>
  mapDispatchToProps
<span class="token punctuation">)</span><span class="token punctuation">(</span>UndoRedo<span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> UndoRedo
</code></pre>
<p>&#x73B0;&#x5728;&#x628A;&#x8FD9;&#x4E2A; <code>UndoRedo</code> &#x7EC4;&#x4EF6;&#x6DFB;&#x52A0;&#x5230; <code>App</code> &#x7EC4;&#x4EF6;&#xFF1A;</p>
<h4 id="-components-app-js"><code>components<span class="token operator">/</span>App<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React from <span class="token string">&apos;react&apos;</span>
<span class="token keyword">import</span> Footer from <span class="token string">&apos;./Footer&apos;</span>
<span class="token keyword">import</span> AddTodo from <span class="token string">&apos;../containers/AddTodo&apos;</span>
<span class="token keyword">import</span> VisibleTodoList from <span class="token string">&apos;../containers/VisibleTodoList&apos;</span>
<span class="token keyword">import</span> UndoRedo from <span class="token string">&apos;../containers/UndoRedo&apos;</span>

<span class="token keyword">const</span> App <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>
  <span class="token operator">&lt;</span>div<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>AddTodo <span class="token operator">/</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>VisibleTodoList <span class="token operator">/</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>Footer <span class="token operator">/</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>UndoRedo <span class="token operator">/</span><span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
<span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> App
</code></pre>
<p>&#x5C31;&#x662F;&#x8FD9;&#x6837;&#xFF01;&#x5728;<a href="https://github.com/reactjs/redux/tree/master/examples/todos-with-undo" target="_blank">&#x793A;&#x4F8B;&#x6587;&#x4EF6;&#x5939;</a>&#x4E0B;&#x6267;&#x884C; <code>npm install</code> &#x548C; <code>npm start</code> &#x8BD5;&#x8BD5;&#x770B;&#x5427;&#xFF01;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../docs/recipes/ComputingDerivedData.html" class="navigation navigation-prev " aria-label="Previous page: 计算衍生数据"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../docs/recipes/IsolatingSubapps.html" class="navigation navigation-next " aria-label="Next page: 子应用隔离"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-ga/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"edit-link":{"base":"https://github.com/camsong/redux-in-chinese/tree/master","label":"开始纠错"},"ga":{"token":"UA-66122997-1","configuration":"auto"}};
    gitbook.start(config);
});
</script>

        <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-66122997-1', 'auto');ga('send', 'pageview');</script>
    </body>
    
</html>
